package com.bw.controller;

import com.bw.domain.Role;
import com.bw.domain.User;
import com.bw.service.UserService;
import com.bw.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.method.P;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Author: zhaijiaqi
 * @Date: 2025/3/13 16:01
 * @Description:
 **/
@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    UserService userService;


    @ResponseBody
    @RequestMapping("add")
    public R add(User user){
        User user1= userService.checkusername(user.getUsername());
        if(user1!=null){
            return R.ERROR(500,"名称重复请更换");
        }
      boolean i= userService.add(user);
      return i?R.OK():R.ERROR(500,"添加失败");
    }




    /**
     * 检查名称是否重复
     * @param username
     * @return
     */
    @ResponseBody
    @RequestMapping("checkusername")
    public R checkusername(String username){
       User user= userService.checkusername(username);

       return user!=null?R.ERROR(500,"用户名已重复"):R.OK();
    }


    /**
     * 查询出所有的角色数据
     * @return
     */
    @ResponseBody
    @RequestMapping("getallrole")
    public R getallrole(){
       List<Role> roleList= userService.getallrole();
       return R.OK(roleList);
    }


    /**
     * 去往添加人员页面
     * @return
     */
    @RequestMapping("toadd")
    public String toadd(){
        return "/user/add";
    }


//    /**
//     * 执行添加人员
//     * @return
//     */
//    @ResponseBody
//    @RequestMapping("add")
//    public R add(){
//
//    }


}
